<?php

namespace App\Api\Http\Requests;

use App\Rules\Phone;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class ItemRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'company_id'    => 'required',
            'project_id'    => 'required',
            'department'    => 'required',
            'contact_name'  => 'required',
            'contact_phone' => ['required', new Phone()],
            'title'         => 'required|max:20',
            'stage'         => 'required|numeric',
            'tag_ids'       => 'array',
            'price'         => 'required|numeric|max:1000|min:0.01',
            'description'   => 'required|max:2000',
            'expired_at'    => 'required|date|after:tomorrow',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'company_id'    => '公司名称',
            'project_id'    => '项目名称',
            'department'    => '专业',
            'contact_name'  => '联系人',
            'contact_phone' => '联系号码',
            'title'         => '标题',
            'stage'         => '阶段',
            'price'         => '价格',
            'description'   => '描述',
            'expired_at'    => '过期日期',
            'tag_ids'       => '标签',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'required'  => ':attribute必填',
            'max'       => ':attribute不能超过:max字',
            'price.max' => '价格不能超过:max元',
            'price.min' => '价格不能低于:min元',
            'numeric'   => ':attribute必须为数字',
            'date'      => ':attribute格式错误',
            'after'     => ':attribute值错误',
            'array'     => '数据错误',
        ];
    }

    /**
     * Get data to be validated from the request.
     *
     * @return array
     */
    protected function validationData()
    {
        return $this->request->all();
    }
}
